package cn.tedu.blog.server.service.impl;

import cn.tedu.blog.server.exception.CoolSharkServiceException;
import cn.tedu.blog.server.exception.ServiceException;
import cn.tedu.blog.server.mapper.UserUserMapper;
import cn.tedu.blog.server.pojo.dto.UserUserAddNewDTO;
import cn.tedu.blog.server.security.LoginPrincipal;
import cn.tedu.blog.server.service.IUserUserService;
import cn.tedu.blog.server.utils.ResponseCode;
import cn.tedu.blog.server.utils.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class UserUserServiceImpl implements IUserUserService {

    @Autowired
    private UserUserMapper userUserMapper;

    //  统计用户关注博主的数量
    @Override
    public int followCountByUserId() {
        // 从jwt上下文中获得用户id
        Long id = getUserId();
        int favoriteSize = userUserMapper.followCountByUserId(id);
        return favoriteSize;
    }

    // 统计关注此用户的其他用户数量
    @Override
    public int followUserCountByUserId() {
        // 从jwt上下文中获得用户id,
        Long id = getUserId();
        int fansSize = userUserMapper.followUserCountByUserId(id);
        return fansSize;
    }

    /*获取用户信息*/
    public LoginPrincipal getUserInfo(){

        UsernamePasswordAuthenticationToken authenticationToken=
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext()
                        .getAuthentication();
        if(authenticationToken==null){
            throw new ServiceException(ServiceCode.ERR_UNKNOWN,"没有登录信息");
        }
        // 如果authenticationToken不为空,获得其中的用户信息
        LoginPrincipal userInfo=(LoginPrincipal) authenticationToken.getPrincipal();
        // 返回登录用户信息
        return  userInfo;
    }
    /*获取用户id*/
    public Long getUserId(){
        return getUserInfo().getId();
    }
    // 新增用户关联数据
    @Override
    public void UserUserAddNew(UserUserAddNewDTO userUserAddNewDTO) {
        // 从jwt上下文中获得用户id
        Long userId = getUserId();
        // 补全数据
        userUserAddNewDTO.setUserId(userId);
        LocalDateTime modified = LocalDateTime.now();
        userUserAddNewDTO.setGmtCreate(modified);
        // 查询是否已经关注
        int count = userUserMapper.selectUserAndUser(userUserAddNewDTO);
        if (count>0){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "您已经关注了该博主!");
        }
        int rows = userUserMapper.insertUserUser(userUserAddNewDTO);
        if (rows != 1){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "关注失败!请稍后重试");
        }
    }

    @Override
    public void deleteUserUserById(UserUserAddNewDTO userUserAddNewDTO) {
        log.info("即将进行删除用户关联表业务,接收数据:{}", userUserAddNewDTO);
        // 从jwt上下文中获得用户id,
        Long id = getUserId();
        //补全数据
        userUserAddNewDTO.setUserId(id);
        int rows = userUserMapper.deleteUserUserById(userUserAddNewDTO);
        if (rows!=1){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "取消关注失败!请稍后重试");
        }
    }

    /*判断是否关注此博客*/
    @Override
    public boolean concerned(UserUserAddNewDTO userUserAddNewDTO){
        UsernamePasswordAuthenticationToken authenticationToken=
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext()
                        .getAuthentication();
        if(authenticationToken!=null){
            // 从jwt上下文中获得用户id,
            Long id = getUserId();
            //补全数据
            userUserAddNewDTO.setUserId(id);
            int count = userUserMapper.selectUserAndUser(userUserAddNewDTO);
            log.info("--------------"+count);
            if (count>0){
                return true;
            }
        }
        return false;
    }

}
